package org.example.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.springboot.common.Result;
import org.example.springboot.entity.Refund;
import org.example.springboot.service.RefundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "退款管理接口")
@RestController
@RequestMapping("/refund")
public class RefundController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RefundController.class);

    @Autowired
    private RefundService refundService;

    @Operation(summary = "申请退款")
    @PostMapping("/apply")
    public Result<?> applyRefund(@RequestBody Refund refund) {
        return refundService.applyRefund(refund);
    }

    @Operation(summary = "处理退款申请")
    @PutMapping("/{id}/process")
    public Result<?> processRefund(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) String merchantRemark) {
        return refundService.processRefund(id, status, merchantRemark);
    }

    @Operation(summary = "获取退款详情")
    @GetMapping("/{id}")
    public Result<?> getRefundById(@PathVariable Long id) {
        return refundService.getRefundById(id);
    }

    @Operation(summary = "获取订单的退款信息")
    @GetMapping("/order/{orderId}")
    public Result<?> getRefundByOrderId(@PathVariable Long orderId) {
        return refundService.getRefundByOrderId(orderId);
    }

    @Operation(summary = "分页查询退款列表")
    @GetMapping("/page")
    public Result<?> getRefundsByPage(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        return refundService.getRefundsByPage(userId, status, currentPage, size);
    }

    @Operation(summary = "获取待处理退款数量")
    @GetMapping("/count/pending")
    public Result<?> getPendingRefundCount(@RequestParam(required = false) Long merchantId) {
        return refundService.getPendingRefundCount(merchantId);
    }
} 